package com.party.service.sys.impl;

import com.party.common.Page;
import com.party.mapper.SysRoleAccessMapper;
import com.party.mapper.SysRoleMapper;
import com.party.model.SysRole;
import com.party.model.SysRoleAccess;
import com.party.model.SysRoleAccessExample;
import com.party.model.SysRoleExample;
import com.party.service.sys.RoleService;
import org.springframework.stereotype.Repository;

import javax.annotation.Resource;
import java.util.List;

/**
 * Created by IntelliJ IDEA.
 * User: Administrator
 * Date: 13-3-31
 * Time: 下午3:16
 * To change this template use File | Settings | File Templates.
 */
@Repository
public class RoleServiceImpl implements RoleService {
    @Resource
    SysRoleMapper sysRoleMapper;
    @Resource
    SysRoleAccessMapper sysRoleAccessMapper;

    public int add(SysRole sysRole) {
        return 0;
    }

    public int del(String id) {
        int num = 0;
        if (id != null) {
            String roids[] = id.split(",");
            for (String roid : roids) {
                num += sysRoleMapper.deleteByPrimaryKey(Integer.parseInt(roid));

                SysRoleAccessExample sysRoleAccessExample = new SysRoleAccessExample();
                SysRoleAccessExample.Criteria criteria = sysRoleAccessExample.createCriteria();
                criteria.andRoleidEqualTo(Integer.parseInt(roid));
                sysRoleAccessMapper.deleteByExample(sysRoleAccessExample);
            }
        }
        return num;
    }

    public SysRole getSysRoleById(int roleid) {
        return sysRoleMapper.selectByPrimaryKey(roleid);
    }

    public Page getList(SysRole sysRole, int pageSize, int pageNumber) {
        SysRoleExample sysRoleExample = new SysRoleExample();
        if(sysRole != null)
        {
            SysRoleExample.Criteria criteria = sysRoleExample.createCriteria();
            if(sysRole.getName() != null && !"".equals(sysRole.getName()))
            {
                criteria.andNameLike("%"+sysRole.getName()+"%");
            }
            if(sysRole.getStatus() != null)
            {
                criteria.andStatusEqualTo(sysRole.getStatus());
            }
        }

        int count = this.sysRoleMapper.countByExample(sysRoleExample);
        sysRoleExample.setBeginNum((pageNumber - 1) * pageSize);
        sysRoleExample.setEndNum(pageSize);
        List<SysRole> list = this.sysRoleMapper.getListPage(sysRoleExample);
        Page page = new Page(pageNumber, count, pageSize, list);
        return page;
    }

    public List<SysRole> getRoles() {
        return sysRoleMapper.selectByExample(new SysRoleExample());
    }

    public int addAccess(String rolename,int roleStatus, String[] nodes) {
        int status = 0;

        SysRoleExample tRoleExample = new SysRoleExample();
        SysRoleExample.Criteria criteria = tRoleExample.createCriteria();
        criteria.andNameEqualTo(rolename);

        int count = sysRoleMapper.countByExample(tRoleExample);
        if (count == 0) {
            SysRole sysRole = new SysRole();
            sysRole.setName(rolename);
            sysRole.setStatus(roleStatus);
            sysRoleMapper.insertSelective(sysRole);

            int roleId = sysRole.getRoleid();

            if (nodes != null) {
                SysRoleAccess sysRoleAccess = new SysRoleAccess();
                for (String nodeId : nodes) {
                    sysRoleAccess.setNodeid(Integer.parseInt(nodeId));
                    sysRoleAccess.setRoleid(roleId);
                    sysRoleAccessMapper.insertSelective(sysRoleAccess);
                }
            }

            status = 1;

        } else {
            status = -2; //存在的角色名
        }

        return status;
    }

    public int updateAccess(SysRole sysRole, String[] nodes) {
        sysRoleMapper.updateByPrimaryKeySelective(sysRole);

        SysRoleAccessExample sysRoleAccessExample = new SysRoleAccessExample();
        SysRoleAccessExample.Criteria criteria = sysRoleAccessExample.createCriteria();
        criteria.andRoleidEqualTo(sysRole.getRoleid());
        sysRoleAccessMapper.deleteByExample(sysRoleAccessExample);

        SysRoleAccess sysRoleAccess = new SysRoleAccess();
        if(nodes!=null){
            for (String nodeId : nodes) {
                sysRoleAccess.setNodeid(Integer.parseInt(nodeId));
                sysRoleAccess.setRoleid(sysRole.getRoleid());
                sysRoleAccessMapper.insertSelective(sysRoleAccess);
            }
        }

        return 1;
    }

    public List<SysRoleAccess> getRoleAccesssByRoleId(int roleId) {
        SysRoleAccessExample sysRoleAccessExample = new SysRoleAccessExample();
        SysRoleAccessExample.Criteria criteria = sysRoleAccessExample.createCriteria();
        criteria.andRoleidEqualTo(roleId);

        List<SysRoleAccess> sysRoleAccesses = sysRoleAccessMapper.selectByExample(sysRoleAccessExample);
        return sysRoleAccesses;
    }
}
